//
//  VisitorView.swift
//  Residual_memmem
//
//  Created by 武振磊 on 16/7/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
    ///   用户登录视图
    ///   - parameter ImageName: ImageName
    ///   - parameter text:      text
    func setUpInfo(ImageName: String?, text: String){
        TextLabel.text = text
        guard let ImageName = ImageName else {
            return
        }
        HouseImage.image = UIImage(named: ImageName)
        IconImage.hidden = true
    }

    //MARK: 初始化函数
    //   纯代码 视图初始化默认函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        setUpUI()
        startAnim()

    }
    ///   SB|Xib初始化入口
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    //圆圈
    private lazy var IconImage: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮盖
    private lazy var MaskIconImage : UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var HouseImage: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //标签
    private lazy var TextLabel: UILabel = UILabel(title: " ", color: UIColor.darkGrayColor(), textFont: 14)
    //注册按钮
    lazy var RegisterButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), font: 14, imageName: "common_button_white_disable")
    //登录按钮
    lazy var LoginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), font: 14, imageName: "common_button_white_disable")
}

// MARK: - 设置UI界面
extension VisitorView{
    
    private func startAnim(){
        ///    开启动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
        
        IconImage.layer.addAnimation(anim, forKey: nil)
    }
    
    ///   设置UI
    private func setUpUI(){
        
        //设置背景色.灰度图
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        //圆圈
        addSubview(IconImage)
        //遮盖
        addSubview(MaskIconImage)
        //小房子
        addSubview(HouseImage)
        //标签
        addSubview(TextLabel)
        //注册按钮
        addSubview(RegisterButton)
        //登录按钮
        addSubview(LoginButton)
        
        //设定这个属性为false，可以进行自动布局，如果设为true，是以setFrame的形式来计算位置
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //圆圈
        addConstraint(NSLayoutConstraint(item: IconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: IconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        //遮盖
        addConstraint(NSLayoutConstraint(item: MaskIconImage, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: MaskIconImage, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: MaskIconImage, attribute: NSLayoutAttribute.Right, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: MaskIconImage, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
        //小房子
        addConstraint(NSLayoutConstraint(item: HouseImage, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: IconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: HouseImage, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: IconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //标签
        addConstraint(NSLayoutConstraint(item: TextLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: IconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: TextLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: IconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: TextLabel, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: TextLabel, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: RegisterButton, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: TextLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: RegisterButton, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: TextLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: RegisterButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: RegisterButton, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Right, relatedBy: .Equal, toItem: TextLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: RegisterButton, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 36))
        
        

    }
}